package com.example.myapplication.module

import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import com.example.myapplication.R
import com.example.myapplication.SecondActivity
import com.example.myapplication.databinding.ActivityMainBinding
import com.example.myapplication.entity.ItemBean
import com.example.myapplication.module.adapter.CategoryListAdapter
import com.example.myapplication.module1.*
import com.example.myapplication.module1.activity.*
import com.example.mylibrary.adapter.BaseFragmentAdapter
import com.example.mylibrary.base.BaseMvpActivity
import com.example.mylibrary.utils.UiTools
import com.google.android.material.tabs.TabLayoutMediator

class MainActivity : BaseMvpActivity<MainActivity, MainPresenter, ActivityMainBinding>() {

    lateinit var tabList: Array<String>
    val customerMangerList = mutableListOf<Any>()
    val pswInfoList = mutableListOf<Any>()
    val cashBankList = mutableListOf<Any>()
    val statisticsList = mutableListOf<Any>()

    private val fragmentList = mutableListOf<Fragment>()

    private val baseFragmentAdapter: BaseFragmentAdapter by lazy {
        BaseFragmentAdapter(this)
    }
    private val categoryListAdapter: CategoryListAdapter by lazy {
        CategoryListAdapter(this)
    }

    override fun initView() {
        tabList = UiTools.getStringArray(R.array.tabCategory)
    }

    val bundle = Bundle()
    override fun initData() {
        // 通过recyclerview直接设置
        getCustomerInfo()
        getPswInfo()
        getCashBank()
        getStatistics()
        categoryListAdapter.categoryClickListener =
            object : CategoryListAdapter.CategoryClickListener {
                override fun onClickListenerCategory(itemBean: ItemBean) {
                    bundle.putParcelable("itemInfo", itemBean)
                    val clazz: Class<*> = when (itemBean.itemIndex) {
                        // 秘书提醒
                        "100" -> {
                            // 工作提醒
                            WorkNoticeActivity::class.java
                        }
                        "101" -> {
                            // 公司公告
                            CompanyNoticeActivity::class.java
                        }
                        "102" -> {
                            // 签到
                            SignInActivity::class.java
                        }
                        "103" -> {
                            // 个人日程
                            PersonalScheduleActivity::class.java
                        }
                        // 工作平台
                        "110" -> {
                            // 计划拜访
                            PlanVisitActivity::class.java
                        }
                        "111" -> {
                            // 预约拜访
                            bundle.putString("params", "jhrwyybf")
                            AppointmentVisitActivity::class.java
                        }
                        "112" -> {
                            // 售后回访
                            bundle.putString("params", "jhrwshhf")
                            AppointmentVisitActivity::class.java
                        }
                        "113" -> {
                            // 客户管理
                            CustomerManagerActivity::class.java
                        }
                        "114" -> {
                            // 来电录入
                            CallEntryActivity::class.java
                        }
                        "115" -> {
                            // 近期新增单位
                            NewCompanyActivity::class.java
                        }
                        "116" -> {
                            // 物流管理
                            LogisticsManagerActivity::class.java
                        }
                        "117" -> {
                            // 销售机会
                            SalesOpportunityActivity::class.java
                        }
                        "118" -> {
                            // 合同管理
                            ContractManagementActivity::class.java
                        }
                        "119" -> {
                            // 项目管理
                            ProjectManagementActivity::class.java
                        }
                        // 安装维修
                        "120" -> {
                            // 安装登记
                            SecondActivity::class.java
                        }
                        "121" -> {
                            // 安装执行
                            SecondActivity::class.java
                        }
                        "122" -> {
                            // 维修登记
                            SecondActivity::class.java
                        }
                        "123" -> {
                            // 检测维修
                            SecondActivity::class.java
                        }
                        // 计划总结
                        "130" -> {
                            // 日计划
                            SecondActivity::class.java
                        }
                        "131" -> {
                            // 周计划
                            SecondActivity::class.java

                        }
                        "132" -> {
                            // 月计划
                            SecondActivity::class.java

                        }
                        "133" -> {
                            // 年计划
                            SecondActivity::class.java

                        }
                        // 进销存
                        "200" -> {
                            // 采购订单
                            SecondActivity::class.java

                        }
                        "201" -> {
                            // 采购收货
                            SecondActivity::class.java

                        }
                        "202" -> {
                            // 采购付款
                            SecondActivity::class.java

                        }
                        "203" -> {
                            // 采购退货
                            SecondActivity::class.java

                        }
                        "210" -> {
                            // 销售订单
                            SecondActivity::class.java

                        }
                        "211" -> {
                            // 销售收货
                            SecondActivity::class.java
                        }
                        "212" -> {
                            // 销售付款
                            SecondActivity::class.java
                        }
                        "213" -> {
                            // 销售退货
                            SecondActivity::class.java
                        }
                        "220" -> {
                            // 仓库调拨
                            SecondActivity::class.java
                        }
                        "221" -> {
                            // 库存盘点
                            SecondActivity::class.java
                        }
                        "222" -> {
                            // 库存报表
                            SecondActivity::class.java
                        }
                        // 现金银行
                        "300" -> {
                            // 费用支出
                            SecondActivity::class.java
                        }
                        "301" -> {
                            // 银行存取
                            SecondActivity::class.java
                        }
                        "302" -> {
                            // 付款单
                            SecondActivity::class.java
                        }
                        "303" -> {
                            // 收款单
                            SecondActivity::class.java
                        }
                        "304" -> {
                            // 其他收入
                            SecondActivity::class.java
                        }
                        // 统计分析
                        "400" -> {
                            // 客户拜访统计
                            SecondActivity::class.java
                        }
                        "401" -> {
                            // 新客户统计
                            SecondActivity::class.java
                        }
                        "402" -> {
                            // 销售机会统计
                            SecondActivity::class.java
                        }
                        "403" -> {
                            // 客户等级统计
                            SecondActivity::class.java
                        }
                        "404" -> {
                            // 销售阶段统计
                            SecondActivity::class.java
                        }
                        "410" -> {
                            // 库存报表
                            SecondActivity::class.java
                        }
                        "411" -> {
                            // 应收应付
                            SecondActivity::class.java
                        }
                        "412" -> {
                            // 资金账户
                            SecondActivity::class.java
                        }
                        "413" -> {
                            // 销售收款
                            SecondActivity::class.java
                        }
                        "414" -> {
                            // 利润表
                            SecondActivity::class.java
                        }
                        "415" -> {
                            // 资产负债表
                            SecondActivity::class.java
                        }
                        else -> {
                            ErrorActivity::class.java
                        }
                    }

                    if (clazz != ErrorActivity::class.java) {
                        startNextActivity(this@MainActivity, bundle, clazz)
                    } else {
                        UiTools.showToast("页面跳转错误")
                    }
                }
            }
        viewBinding.vp.adapter = categoryListAdapter
        categoryListAdapter.setData(
            tabList,
            customerMangerList,
            pswInfoList,
            cashBankList,
            statisticsList
        )

        // 通过fragment设置
//        vp.adapter = baseFragmentAdapter
//        fragmentList.add(CustomerManagerFragment())
//        fragmentList.add(PswFragment())
//        fragmentList.add(CashBankFragment())
//        fragmentList.add(StatisticsFragment())
//        baseFragmentAdapter.fragmentList = fragmentList

        // 禁用viewpager手动滑动切换
//        vp.isUserInputEnabled = false
        viewBinding.vp.offscreenPageLimit = 1


        // 绑定viewpager和tablayout联动
        TabLayoutMediator(
            viewBinding.tlCategory, viewBinding.vp
        ) { tab, position -> tab.text = tabList[position] }.attach()


    }

    override fun getBinding(): ActivityMainBinding {
        return ActivityMainBinding.inflate(layoutInflater)
    }

    override fun onClick(p0: View?) {

    }

    override fun getPresenter(): MainPresenter = MainPresenter()

    // 获取客户管理界面
    private fun getCustomerInfo() {
        customerMangerList.clear()
        val itemBeanList = mutableListOf<ItemBean>()
        val serviceNotice = UiTools.resources.obtainTypedArray(R.array.secretaryImage)
        val workPlatformArray = UiTools.resources.obtainTypedArray(R.array.workPlatformImage)
        val installFixed = UiTools.resources.obtainTypedArray(R.array.installRepairImage)
        val plan = UiTools.resources.obtainTypedArray(R.array.planSummaryImage)

        val customerManagerTitle = UiTools.getStringArray(R.array.customerManagerTitle)
        val secretary = UiTools.getStringArray(R.array.secretary)
        val workPlatform = UiTools.getStringArray(R.array.workPlatform)
        val installRepair = UiTools.getStringArray(R.array.installRepair)
        val planSummary = UiTools.getStringArray(R.array.planSummary)

        for (i in customerManagerTitle.indices) {
            itemBeanList.clear()
            customerMangerList.add(customerManagerTitle[i])
            when (i) {
                0 -> {
                    for (index in secretary.indices) {
                        itemBeanList.add(
                            ItemBean(
                                1,
                                i,
                                index,
                                "1$i$index",
                                secretary[index],
                                serviceNotice.getResourceId(index, 0)
                            )
                        )
                    }
                }
                1 -> {
                    for (index in workPlatform.indices) {
                        itemBeanList.add(
                            ItemBean(
                                1,
                                i,
                                index,
                                "1$i$index",
                                workPlatform[index],
                                workPlatformArray.getResourceId(index, 0)
                            )
                        )
                    }
                }
                2 -> {
                    for (index in installRepair.indices) {
                        itemBeanList.add(
                            ItemBean(
                                1,
                                i,
                                index,
                                "1$i$index",
                                installRepair[index],
                                installFixed.getResourceId(index, 0)
                            )
                        )
                    }
                }
                3 -> {
                    for (index in planSummary.indices) {
                        itemBeanList.add(
                            ItemBean(
                                1,
                                i,
                                index,
                                "1$i$index",
                                planSummary[index],
                                plan.getResourceId(index, 0)
                            )
                        )
                    }
                }
            }
            customerMangerList.addAll(itemBeanList)
        }
        serviceNotice.recycle()
        workPlatformArray.recycle()
        installFixed.recycle()
        plan.recycle()
    }

    // 获取进销存
    private fun getPswInfo() {
        val itemBeanList = mutableListOf<ItemBean>()
        pswInfoList.clear()
        val pswTitle = UiTools.getStringArray(R.array.pswTitle)
        val buyManager = UiTools.getStringArray(R.array.buyManager)
        val sellManager = UiTools.getStringArray(R.array.sellManager)
        val warehouseManager = UiTools.getStringArray(R.array.warehouseManager)
        val buyManagerImage = UiTools.resources.obtainTypedArray(R.array.buyManagerImage)
        val sellManagerImage = UiTools.resources.obtainTypedArray(R.array.sellManagerImage)
        val warehouseManagerImage =
            UiTools.resources.obtainTypedArray(R.array.warehouseManagerImage)

        for (i in pswTitle.indices) {
            itemBeanList.clear()
            pswInfoList.add(pswTitle[i])
            when (i) {
                0 -> {
                    for (index in buyManager.indices) {
                        itemBeanList.add(
                            ItemBean(
                                2,
                                i,
                                index,
                                "2$i$index",
                                buyManager[index],
                                buyManagerImage.getResourceId(index, 0)
                            )
                        )
                    }
                }
                1 -> {
                    for (index in sellManager.indices) {
                        itemBeanList.add(
                            ItemBean(
                                2,
                                i,
                                index,
                                "2$i$index",
                                sellManager[index],
                                sellManagerImage.getResourceId(index, 0)
                            )
                        )
                    }
                }
                2 -> {
                    for (index in warehouseManager.indices) {
                        itemBeanList.add(
                            ItemBean(
                                2,
                                i,
                                index,
                                "2$i$index",
                                warehouseManager[index],
                                warehouseManagerImage.getResourceId(index, 0)
                            )
                        )
                    }
                }
            }
            pswInfoList.addAll(itemBeanList)
        }
        buyManagerImage.recycle()
        sellManagerImage.recycle()
        warehouseManagerImage.recycle()
    }

    private fun getCashBank() {
        cashBankList.clear()
        val itemBeanList = mutableListOf<ItemBean>()
        val cashBank = UiTools.getStringArray(R.array.cashBank)
        val cashBankImage = UiTools.resources.obtainTypedArray(R.array.cashBankImage)

        for (index in cashBank.indices) {
            itemBeanList.clear()
            itemBeanList.add(
                ItemBean(
                    3,
                    0,
                    index,
                    "30$index",
                    cashBank[index],
                    cashBankImage.getResourceId(index, 0)
                )
            )
            cashBankList.addAll(itemBeanList)
        }
        cashBankImage.recycle()
    }

    private fun getStatistics() {
        statisticsList.clear()
        val itemBeanList = mutableListOf<ItemBean>()
        val statisticsTitle = UiTools.getStringArray(R.array.statisticsTitle)
        val statisticsCustomer = UiTools.getStringArray(R.array.statisticsCustomer)
        val financeStatistics = UiTools.getStringArray(R.array.financeStatistics)
        val statisticsCustomerImage =
            UiTools.resources.obtainTypedArray(R.array.statisticsCustomerImage)
        val financeStatisticsImage =
            UiTools.resources.obtainTypedArray(R.array.financeStatisticsImage)

        for (i in statisticsTitle.indices) {
            itemBeanList.clear()
            statisticsList.add(statisticsTitle[i])
            when (i) {
                0 -> {
                    for (index in statisticsCustomer.indices) {
                        itemBeanList.add(
                            ItemBean(
                                4,
                                i,
                                index,
                                "4$i$index",
                                statisticsCustomer[index],
                                statisticsCustomerImage.getResourceId(index, 0)
                            )
                        )
                    }
                }
                1 -> {
                    for (index in financeStatistics.indices) {
                        itemBeanList.add(
                            ItemBean(
                                4,
                                i,
                                index,
                                "4$i$index",
                                financeStatistics[index],
                                financeStatisticsImage.getResourceId(index, 0)
                            )
                        )
                    }
                }
            }
            statisticsList.addAll(itemBeanList)
        }
        statisticsCustomerImage.recycle()
        financeStatisticsImage.recycle()
    }

}